<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/11 0011
 * Time: 16:32
 */

namespace Cohuna\Controllers;

use Cohuna\Models\CohunaOrder;
use Cohuna\Models\CohunaOrderDetail;
use Cohuna\Paginator\PaginatorArray;
use Cohuna\Services\TransactionService;

/**
 * 交易管理
 * @package Cohuna\Controllers
 */
class TransactionController extends ControllerBase
{
    /**
     * 交易记录列表
     */
    public function listAction()
    {
        $trade_channel_list = $this->config->trade_channel_list->toArray();
        $trade_state_list  = $this->config->trade_state_list->toArray();

        $this->view->setVar('trade_state_list',$trade_state_list);
        $this->view->setVar('trade_channel_list',$trade_channel_list);

        $search_field = $this->request->getQuery('search_field');
        $search_keyword = trim($this->request->getQuery('search_keyword'));
        $trade_status = trim($this->request->getQuery('trade_status'));
        $trade_channel = trim($this->request->getQuery('trade_channel'));
        $pay_time_start = $this->request->getQuery('pay_time_start');
        $pay_time_end = $this->request->getQuery('pay_time_end');
        $page = max(intval($this->request->getQuery('page')),1);

        $condition = 'order_id > 0';
        $params = array();
        if(in_array($search_field,array('transaction_id','out_transaction_id','order_no')) && empty($search_keyword) === false){
            $condition .= ' AND ' . $search_field . ' = :keyword:';
            $params['keyword'] = $search_keyword;
        }else{
            $search_keyword = '';
        }
        if(isset($trade_state_list[$trade_status])){
            $condition .= ' AND trade_state=:trade_state:';
            $params['trade_state'] = $trade_status;
        }
        if(empty($trade_channel) === false && isset($trade_channel_list[$trade_channel])){
            $condition .= ' AND trade_channel=:trade_channel:';
            $params['trade_channel'] = $trade_channel;

        }

       if(empty($pay_time_start) === false && $start_time = strtotime($pay_time_start)){
            $condition .= ' AND time_end >= :time_end:';
            $params['time_end'] = date('Y-m-d H:i:s',$start_time);
       }
        if(empty($pay_time_end) === false && $end_time = strtotime($pay_time_end)){
            $condition .= ' AND time_end <= :time_end:';
            $params['time_end'] = date('Y-m-d H:i:s',$end_time);
        }

        $conditions = array($condition,'bind'=>$params);

        $paginator = TransactionService::getInstance()->getOrderList($conditions,$page);

        $this->view->setVar("search_field",$search_field);
        $this->view->setVar('search_keyword',$search_keyword);
        $this->view->setVar('trade_status',$trade_status);
        $this->view->setVar('trade_channel',$trade_channel);
        $this->view->setVar('pay_time_start',$pay_time_start);
        $this->view->setVar('pay_time_end',$pay_time_end);
        $this->view->setVar('Model',$paginator);
    }

    /**
     * 交易详情
     */
    public function detailAction()
    {
        $order_no = trim($this->request->getQuery('order_no'));

        $order = CohunaOrder::findFirstByOrderNo($order_no);
        if(empty($order_no)){
            return $this->dispatcher->forward(array('controler'=>'Error','action'=>'show404'));
        }
        $order_detail_list = CohunaOrderDetail::find(array(
            'order_no = :order_no:',
            'bind' => array('order_no' => $order_no)
        ));

        $this->view->setVar('Order',CohunaOrder::getCohunaOrderExtendInfo($order));
        $this->view->setVar('OrderDetailList',$order_detail_list);
    }
}